package org.jeecg.modules.cosmetic.handler;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.ocr.v20181119.OcrClient;
import com.tencentcloudapi.ocr.v20181119.models.GeneralAccurateOCRRequest;
import com.tencentcloudapi.ocr.v20181119.models.GeneralAccurateOCRResponse;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.cosmetic.model.vo.PictureOcrVO;
import org.jeecg.modules.cosmetic.model.vo.PicturePathVO;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import java.net.URLDecoder;


@Service
@Slf4j
public class PictureOCRHandler {

    @Value("${ocr.secretId}")
    private String secretId;

    @Value("${ocr.secretKey}")
    private String secretKey;

    /**
     * 通过图片地址解析图片，实际的生产环境使用
     * @param vo
     * @return
     */
    public Result<?> scanPathPicture(PicturePathVO vo) {
        if (StringUtils.isBlank(vo.getPath())) {
            log.error("图片地址为空");
            return Result.error("图片地址为空");
        }
        StringBuilder stringBuilder = new StringBuilder();
        PictureOcrVO pictureOcrVO = new PictureOcrVO();
        try {
            // 腾讯 OCR
            Credential cred = new Credential(secretId, secretKey);
            HttpProfile httpProfile = new HttpProfile();
            httpProfile.setEndpoint("ocr.tencentcloudapi.com");
            ClientProfile clientProfile = new ClientProfile();
            clientProfile.setHttpProfile(httpProfile);
            OcrClient client = new OcrClient(cred, "ap-shanghai", clientProfile);
            GeneralAccurateOCRRequest req = new GeneralAccurateOCRRequest();
            req.setImageUrl(URLDecoder.decode(vo.getPath(), "UTF-8"));
            req.setIsPdf(true);
            GeneralAccurateOCRResponse resp = client.GeneralAccurateOCR(req);
            String jsonString = GeneralAccurateOCRResponse.toJsonString(resp);
            // 解析数据
            JSONObject parse = (JSONObject) JSON.parse(jsonString);
            JSONArray jsonArray = parse.getJSONArray("TextDetections");
            for (Object o : jsonArray) {
                JSONObject ob = (JSONObject) o;
                String detectedText = ob.getString("DetectedText");
                stringBuilder.append(detectedText);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        pictureOcrVO.setPictureResult(stringBuilder.toString());
        return Result.OK(pictureOcrVO);
    }

    /**
     * 通过Base64解析数据，前后端联调的时候使用
     * @return
     */
    public Result<?> scanBase64Picture(String base64) {
        if (StringUtils.isBlank(base64)) {
            log.error("图片Base64码为空");
            return Result.error("图片Base64码为空");
        }
        StringBuilder stringBuilder = new StringBuilder();
        PictureOcrVO pictureOcrVO = new PictureOcrVO();
        try {
            // 腾讯 OCR
            Credential cred = new Credential(secretId, secretKey);
            HttpProfile httpProfile = new HttpProfile();
            httpProfile.setEndpoint("ocr.tencentcloudapi.com");
            ClientProfile clientProfile = new ClientProfile();
            clientProfile.setHttpProfile(httpProfile);
            OcrClient client = new OcrClient(cred, "ap-shanghai", clientProfile);
            GeneralAccurateOCRRequest req = new GeneralAccurateOCRRequest();
            req.setImageBase64(base64);
            req.setIsPdf(true);
            GeneralAccurateOCRResponse resp = client.GeneralAccurateOCR(req);
            String jsonString = GeneralAccurateOCRResponse.toJsonString(resp);
            // 解析数据
            JSONObject parse = (JSONObject) JSON.parse(jsonString);
            JSONArray jsonArray = parse.getJSONArray("TextDetections");
            for (Object o : jsonArray) {
                JSONObject ob = (JSONObject) o;
                String detectedText = ob.getString("DetectedText");
                stringBuilder.append(detectedText);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        pictureOcrVO.setPictureResult(stringBuilder.toString());
        return Result.OK(pictureOcrVO);
    }
}
